
<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
        <el-form-item label="订单编号:" prop="orderNumber">
          <el-input v-model="formData.orderNumber" :clearable="true"  placeholder="请输入订单编号" />
       </el-form-item>
        <el-form-item label="商品名称:" prop="goodName">
          <el-input v-model="formData.goodName" :clearable="true"  placeholder="请输入商品名称" />
       </el-form-item>
        <el-form-item label="商品图片:" prop="goodImg">
          <el-input v-model="formData.goodImg" :clearable="true"  placeholder="请输入商品图片" />
       </el-form-item>
        <el-form-item label="商品价格:" prop="goodPrice">
          <el-input-number v-model="formData.goodPrice" :precision="2" :clearable="true"></el-input-number>
       </el-form-item>
        <el-form-item label="商品数量:" prop="goodNum">
          <el-input v-model.number="formData.goodNum" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="收货人:" prop="getPeople">
          <el-input v-model="formData.getPeople" :clearable="true"  placeholder="请输入收货人" />
       </el-form-item>
        <el-form-item label="收货地址:" prop="getAddress">
          <el-input v-model="formData.getAddress" :clearable="true"  placeholder="请输入收货地址" />
       </el-form-item>
        <el-form-item label="收货手机号:" prop="getTel">
          <el-input v-model="formData.getTel" :clearable="true"  placeholder="请输入收货手机号" />
       </el-form-item>
        <el-form-item label="总价:" prop="totalPrice">
          <el-input-number v-model="formData.totalPrice" :precision="2" :clearable="true"></el-input-number>
       </el-form-item>
        <el-form-item label="运费:" prop="shippingFee">
          <el-input-number v-model="formData.shippingFee" :precision="2" :clearable="true"></el-input-number>
       </el-form-item>
        <el-form-item label="订单状态:" prop="orderSataus">
           <el-select v-model="formData.orderSataus" placeholder="请选择订单状态" style="width:100%" :clearable="true" >
              <el-option v-for="(item,key) in orderstatusOptions" :key="key" :label="item.label" :value="item.value" />
           </el-select>
       </el-form-item>
        <el-form-item label="配送状态:" prop="deliveryStatus">
           <el-select v-model="formData.deliveryStatus" placeholder="请选择配送状态" style="width:100%" :clearable="true" >
              <el-option v-for="(item,key) in songstatusOptions" :key="key" :label="item.label" :value="item.value" />
           </el-select>
       </el-form-item>
        <el-form-item label="支付方式:" prop="payType">
           <el-select v-model="formData.payType" placeholder="请选择支付方式" style="width:100%" :clearable="true" >
              <el-option v-for="(item,key) in paystatusOptions" :key="key" :label="item.label" :value="item.value" />
           </el-select>
       </el-form-item>
        <el-form-item label="支付状态:" prop="payStatus">
           <el-select v-model="formData.payStatus" placeholder="请选择支付状态" style="width:100%" :clearable="true" >
              <el-option v-for="(item,key) in paystatusOptions" :key="key" :label="item.label" :value="item.value" />
           </el-select>
       </el-form-item>
        <el-form-item>
          <el-button :loading="btnLoading" type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {
  createLsOrder,
  updateLsOrder,
  findLsOrder
} from '@/api/orders/lsOrder'

defineOptions({
    name: 'LsOrderForm'
})

// 自动获取字典
import { getDictFunc } from '@/utils/format'
import { useRoute, useRouter } from "vue-router"
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'


const route = useRoute()
const router = useRouter()

// 提交按钮loading
const btnLoading = ref(false)

const type = ref('')
const songstatusOptions = ref([])
const paystatusOptions = ref([])
const orderstatusOptions = ref([])
const formData = ref({
            orderNumber: '',
            goodName: '',
            goodImg: '',
            goodPrice: 0,
            goodNum: undefined,
            getPeople: '',
            getAddress: '',
            getTel: '',
            totalPrice: 0,
            shippingFee: 0,
            orderSataus: '',
            deliveryStatus: '',
            payType: '',
            payStatus: '',
        })
// 验证规则
const rule = reactive({
               goodName : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               goodImg : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               goodPrice : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               goodNum : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               getPeople : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               getAddress : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               getTel : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
})

const elFormRef = ref()

// 初始化方法
const init = async () => {
 // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findLsOrder({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
    songstatusOptions.value = await getDictFunc('songstatus')
    paystatusOptions.value = await getDictFunc('paystatus')
    orderstatusOptions.value = await getDictFunc('orderstatus')
}

init()
// 保存按钮
const save = async() => {
      btnLoading.value = true
      elFormRef.value?.validate( async (valid) => {
         if (!valid) return btnLoading.value = false
            let res
           switch (type.value) {
             case 'create':
               res = await createLsOrder(formData.value)
               break
             case 'update':
               res = await updateLsOrder(formData.value)
               break
             default:
               res = await createLsOrder(formData.value)
               break
           }
           btnLoading.value = false
           if (res.code === 0) {
             ElMessage({
               type: 'success',
               message: '创建/更改成功'
             })
           }
       })
}

// 返回按钮
const back = () => {
    router.go(-1)
}

</script>

<style>
</style>
